package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.domain.Destination;
import cn.tedu.boyou.acticle.domain.StrategyDetail;
import cn.tedu.boyou.acticle.service.*;
import cn.tedu.boyou.acticle.vo.StrategyStatisVO;
import cn.tedu.boyou.cache.prefix.StrategyStatisKeyPrefix;
import cn.tedu.boyou.cache.service.IStrategyStatisRedisService;
import cn.tedu.boyou.comment.domain.StrategyComment;
import cn.tedu.boyou.comment.service.IStrategyCommentService;
import cn.tedu.boyou.common.query.StrategyCommentQuery;
import cn.tedu.boyou.common.query.StrategyDetailQuery;
import cn.tedu.boyou.common.util.AjaxResult;
import cn.tedu.boyou.member.domain.UserInfo;
import cn.tedu.boyou.search.query.SearchQueryObject;
import cn.tedu.boyou.search.service.IStrategySearchService;
import cn.tedu.boyou.search.template.StrategyTemplate;
import cn.tedu.boyou.search.vo.StatisVO;
import cn.tedu.boyou.website.annotation.RequireLogin;
import cn.tedu.boyou.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;


    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping("/list")
    public String list(Model model, Long destId, @ModelAttribute("qo") StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(destId);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("tags",strategyTagService.listAll());
        return "strategy/list";
    }

    @RequestMapping("")
    public String index(Model model){
        //所有的推荐排行数据
        List<StrategyStatisVO> all = strategyStatisRedisService.getAllStrategyCommends();

        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        //测试
        abroadCds.add(new StrategyStatisVO(true, 5L, "destName巴拿马", "title椰子树", 4L, 99, 899, 8855, 5656, 66));
        abroadCds.add(new StrategyStatisVO(true, 2L, "destName成都", "title宽窄巷子", 8L, 99, 899, 8855, 5656, 66));
        unabroadCds.add(new StrategyStatisVO(true, 3L, "destName沙漠", "title绿洲", 4L, 99, 899, 8855, 5656, 666));
        unabroadCds.add(new StrategyStatisVO(true, 6L, "destName河马", "title生鲜", 4L, 99, 899, 8855, 5656, 6666));
        
        
        for (StrategyStatisVO vo : all) {
            if(vo.isIsabroad()){
                if(abroadCds.size() < 10){
                    abroadCds.add(vo);
                }
            }else{
                if(unabroadCds.size() < 10){
                    unabroadCds.add(vo);
                }
            }
        }
        //abroadCds 海外的攻略推荐(前10条)
        model.addAttribute("unabroadCds",unabroadCds);
        //unabroadCds 国内的攻略推荐(前10条)
        model.addAttribute("abroadCds",abroadCds);

        //List<Stcm> getStrategyHotCommends() 阅读+评论
        //hotCds 热门的攻略推荐
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getStrategyHotCommends();
//        model.addAttribute("hotCds",hotCds.subList(0,10));
        //model.addAttribute("hotCds",abroadCds);//此条微测试临时添加20200412
        
        
        
        //commends 前五
        //model.addAttribute("commends",strategyCommendService.getCommendsByTop5());
        
        
        //测试
        List<Map<String,Object>> themeCds = new ArrayList<>();
        List<StatisVO> chinas = new ArrayList<>();
        List<StatisVO> abroads = new ArrayList<>();
        List<StatisVO> themes = new ArrayList<>();
        chinas.add(new StatisVO(1L,"热门",1L)); 
        abroads.add(new StatisVO(2L,"冷门",2L)); 
        themes.add(new StatisVO(3L,"热冷门",3L)); 
        
      //测试临时关闭
        //主题攻略推荐
        //themeCds
//        List<Map<String,Object>> themeCds =strategySearchService.queryThemeCommends();
        
        model.addAttribute("themeCds",themeCds);

        //chinas 国内：按省份来分组
//        List<StatisVO> chinas = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_UN_ABROAD); //1
        model.addAttribute("chinas",chinas);
        //abroads 国外: 按国家来分组
//        List<StatisVO> abroads = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_ABROAD); //0
        model.addAttribute("abroads",abroads);
        //themes 主题：按主题来分组
//        List<StatisVO> themes = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_THEME);  //2
        model.addAttribute("themes",themes);

        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo")SearchQueryObject qo){
        Page<StrategyTemplate> page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }



    @RequestMapping("/detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo){
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail",detail);

        //统计阅读数量,仅仅 + 1
        //分享数
        strategyStatisRedisService.viewIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,id);
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,id, 1);
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(id);
        model.addAttribute("vo",statisVo);
        //判断用户是否以及点击
        if(userInfo != null){
            boolean ret = strategyStatisRedisService.isFavornumBySid(id,userInfo.getId());
            model.addAttribute("isFavor",ret);
        }
        return "strategy/detail";
    }

    /**
     * 分享加分
     * @param sid
     * @param model
     * @return
     */
    @RequestMapping("/share")
    @ResponseBody
    @RequireLogin
    public Object share(Long sid,Model model){
        strategyStatisRedisService.shareNum(StrategyStatisKeyPrefix.STRATEGY_STATIS_SHARE, sid);
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(sid);
        return new AjaxResult().addData(statisVo.getSharenum());
    }

    @RequestMapping("/commentAdd")
    @RequireLogin
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.save(comment);

        //统计评论
        //replyIncrease comment.getDestId()
        strategyStatisRedisService.replyIncrease(comment.getDetailId());
        //需要修改页面上的评论数，所以需要将reids中评论数返回页面
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,comment.getDetailId(), 1);
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(comment.getDetailId());
        return  new AjaxResult().addData(statisVo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo")StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    @RequireLogin
    public Object commentThumbUp(Model model,String toid,Long fromid){
        boolean ret = strategyCommentService.thumbUp(toid,fromid);

        return new AjaxResult();
    }


    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Model model,Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = strategyStatisRedisService.favor(StrategyStatisKeyPrefix.STRATEGY_STATIS_FAVOR,sid,userInfo.getId());
        //为true的时候分数加一，为false的时候-1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,ret?1:-1);

        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(sid);
        AjaxResult ajaxResult = new AjaxResult(ret, "").addData(statisVo);
        return ajaxResult;
    }
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Model model,Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = strategyStatisRedisService.thumbup(StrategyStatisKeyPrefix.STRATEGY_STATIS_THUMBSUP,sid,userInfo.getId());
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(sid);
        //顶一天只能顶一次
        if(ret){
            //参数：攻略Id
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid, 1);
        }
        AjaxResult ajaxResult = new AjaxResult(ret, "").addData(statisVo);
        return ajaxResult;
    }
}
